package com.util;


import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;

/*
* 自定义开关控制Log
* by Arno
* 2017年11月23日13:12:12
* */
public class LogUtil {
    //TODO LOG 开关
    private static final boolean isLog = true;



    /**
     * @param tag TAG
     * @param msg 内容
     * @name 自定义Log.i
     * @author ArnoFrost
     * @time 2017/11/30 0030  16:24
     */
    public static void i(String tag, String msg) {
        if (isLog) {
            setAdapterSimple(tag);
            Logger.i(msg);
        }
    }

    public static void i(String tag, String msg, Throwable tr) {
        if (isLog) {
            Logger.i(tag, msg, tr);
        }
    }

    /**
     * @param tag TAG
     * @param msg 内容
     * @name 自定义Log.d
     * @author ArnoFrost
     * @time 2017/11/30 0030  16:24
     */
    public static void d(String tag, String msg) {
        if (isLog) {
            setAdapterSimple(tag);
            Logger.d(msg);
        }
    }

    public static void d(String tag, String msg, Throwable tr) {
        if (isLog) {
            Logger.d(tag, msg, tr);
        }
    }

    /**
     * @param tag TAG
     * @param msg 内容
     * @name 自定义Log.e
     * @author ArnoFrost
     * @time 2017/11/30 0030  16:24
     */
    public static void e(String tag, String msg) {
        if (isLog) {
            setAdapterSimple(tag);
            Logger.e(msg);
        }
    }

    public static void e(String tag, String msg, Throwable tr) {
        if (isLog) {
            Logger.e(tag, msg, tr);
        }
    }

    /**
     * @param tag TAG
     * @param msg 内容
     * @name 自定义Log.v
     * @author ArnoFrost
     * @time 2017/11/30 0030  16:24
     */
    public static void v(String tag, String msg) {
        if (isLog) {
            setAdapterSimple(tag);
            Logger.v(msg);
        }
    }

    public static void v(String tag, String msg, Throwable tr) {
        if (isLog) {
            Logger.v(tag, msg, tr);
        }
    }

    /**
     * @param tag TAG
     * @param msg 内容
     * @name 自定义Log.wtf
     * @author ArnoFrost
     * @time 2017/11/30 0030  16:24
     */
    public static void w(String tag, String msg) {
        if (isLog) {
            setAdapterSimple(tag);
            Logger.w(msg);
        }
    }

    public static void w(String tag, String msg, Throwable tr) {
        if (isLog) {
            Logger.w(tag, msg, tr);
        }
    }

    /**
     * @param tag           标签
     * @param url           请求接口地址
     * @param head          请求头
     * @param headmd5       请求头MD5
     * @param params        请求参数
     * @param urlWithParams 请求数据路径
     */
    public static void p(String tag, String url, String head, String headmd5, String params, String urlWithParams) {
        if (isLog) {

            setAdapterSimple(tag);
            Logger.i("url === " + url);
            Logger.i("head === " + head);
            Logger.i("headmd5 === " + headmd5);
            Logger.i("params === " + params);
            Logger.i("urlWithParams === " + url + "?" + urlWithParams);

        }
    }

    /**
     * @param tag  TAG
     * @param json json字符串
     * @name 自定义Log.json
     * @author ArnoFrost
     * @time 2017/11/30 0030  16:24
     */
    public static void json(String tag, String json) {
        if (isLog) {
            setAdapterWithThread(tag);
            Logger.json(json);
        }
    }

    /**
     * @param tag TAG
     * @param xml xml字符串
     * @name 自定义Log.xml
     * @author ArnoFrost
     * @time 2017/11/30 0030  16:24
     */
    public static void xml(String tag, String xml) {
        if (isLog) {
            setAdapterWithThread(tag);
            Logger.json(xml);
        }
    }

    /**
     * @param tag TAG
     * @name 自定义设置简单LoggerAdapter
     * @author ArnoFrost
     * @time 2017/11/30 0030  16:26
     */
    private static void setAdapterSimple(String tag) {
        Logger.clearLogAdapters();
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // (Optional) Whether to show thread info or not. Default true
                .methodCount(0)         // (Optional) How many method line to show. Default 2
                .methodOffset(7)        // (Optional) Hides internal method calls up to offset. Default 5
                .tag(tag)   // (Optional) Global tag for every log. Default PRETTY_LOGGER
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return super.isLoggable(priority, tag);
//                    return BuildConfig.DEBUG;
            }
        });
    }

    /**
     * @param tag TAG
     * @name 自定义设置复杂LoggerAdapter
     * @author ArnoFrost
     * @time 2017/11/30 0030  16:26
     */
    private static void setAdapterWithThread(String tag) {
        Logger.clearLogAdapters();
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(true)  // (Optional) Whether to show thread info or not. Default true
                .methodCount(4)         // (Optional) How many method line to show. Default 2
                .methodOffset(0)        // (Optional) Hides internal method calls up to offset. Default 5
                .tag(tag)   // (Optional) Global tag for every log. Default PRETTY_LOGGER
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return super.isLoggable(priority, tag);
//                    return BuildConfig.DEBUG;
            }
        });
    }
}

